package com.example.jdbcdemo.service.Impl;

import com.alibaba.fastjson.JSON;
import com.example.jdbcdemo.mapper.UserMapper;
import com.example.jdbcdemo.pojo.User;
import com.example.jdbcdemo.service.TokenInterface;
import com.example.jdbcdemo.service.UserServiceInterface;
import com.example.jdbcdemo.utils.JsonUtil;
import com.example.jdbcdemo.utils.LoginJsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserServiceInterface {
    @Autowired
    UserMapper userMapper;
    @Autowired
    TokenInterface tokenInterface;

    @Override
    public LoginJsonUtil queryUserLoginJson(String userName, String passWord) {
        LoginJsonUtil jsonUtil=new LoginJsonUtil();
        User user= userMapper.queryUserByname(userName);
        if (user==null){
            jsonUtil=new LoginJsonUtil(400,"用户名或密码不正确","","");
            return jsonUtil;
        }
        if (passWord.equals(user.getPassword())){
            String tokenStr=tokenInterface.tokenInSign(new User(0,userName,passWord,user.getUserType()));
            jsonUtil=new LoginJsonUtil(0,"登录成功", JSON.toJSONString(user),tokenStr);
            return jsonUtil;
        }
        else{
            jsonUtil=new LoginJsonUtil(401,"用户名或密码不正确","","");
            return jsonUtil;
        }
    }

    @Override
    public JsonUtil queryUserSignUpJson(String userName, String passWord,int userType) {
        JsonUtil jsonUtil=new JsonUtil();
        User user= userMapper.queryUserByname(userName);
        if (user!=null){
            jsonUtil=new JsonUtil(403,"该用户已存在","");
            return jsonUtil;
        }
        else {
            userMapper.addUser(new User(0,userName,passWord,userType));
            jsonUtil=new JsonUtil(200,"注册成功","");
            return jsonUtil;
        }
    }

    @Override
    public JsonUtil queryUserList() {
        List<User> userList= userMapper.queryUserList();
        List<Map<String,Object>> mapList=new LinkedList<>();
        for (User user:userList)
        {
            Map<String,Object> tempmap=new LinkedHashMap<>();
            tempmap.put("userId",user.getId());
            tempmap.put("userName",user.getName());
            int type=user.getUserType();
            String types="";
            tempmap.put("userType",type);
            switch (type){
                case 0:
                    types="老板";
                    break;
                case 1:
                    types="经理";
                    break;
                case 2:
                    types="销售员";
                    break;
                case 3:
                    types="进货员";
                    break;
                default:
                    break;
            }
            tempmap.put("role",types);
            mapList.add(tempmap);
        }
        return new JsonUtil(0,"查询成功",JSON.toJSONString(mapList));
    }
}
